package com.ddwl.gate.service;

import cn.hutool.core.util.StrUtil;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.exception.ParamException;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.gate.dao.AutoRefundConfigDao;
import com.ddwl.gate.dao.MobileRechargeConfigDao;
import com.ddwl.schema.bo.gate.AutoRefundConfigBo;
import com.ddwl.schema.vo.gate.AutoRefundConfigVo;
import com.ddwl.gate.model.AutoRefundConfig;
import com.ddwl.gate.model.MobileNumInfo;
import com.ddwl.gate.model.MobileRechargeConfig;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.gate.MobileRechargeConfigBo;
import com.ddwl.schema.bo.gate.MobileRechargeConfigPageBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.gate.MobileOperatorConfigVo;
import com.ddwl.schema.vo.gate.MobileRechargeConfigVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author hemiaojun
 * @Date 2020/9/23 9:27
 */
@Service
public class MobileRechargeConfigService {

    @Resource
    private MobileRechargeConfigDao mobileRechargeConfigDao;
    @Resource
    private OperatorConfigService operatorConfigService;
    @Resource
    private MobileNumInfoService mobileNumInfoService;


    @Transactional(rollbackFor = Exception.class)
    public void addConfig(MobileRechargeConfigBo bo) {
        MobileRechargeConfig exist=getByItemId(bo.getItemId());
        ParamException.isNotNull(exist,"该编号商品已存在");
        MobileRechargeConfig entity=BeanConverUtils.convertBean(bo,MobileRechargeConfig.class);
        entity.setId(UUIDGenerator.getShortUUID());
        entity.setEnable(BaseEnum.DflagEnum.NOT_DELETE.getCode());
        mobileRechargeConfigDao.insert(entity);
    }

    public MobileRechargeConfig getByItemId(String itemId) {
        MobileRechargeConfig entity=new MobileRechargeConfig();
        entity.setItemId(itemId);
        return mobileRechargeConfigDao.getByCondition(entity);
    }

    public PageVo<MobileRechargeConfigVo> page(PageBo<MobileRechargeConfigPageBo> pageBo) {
        PageHelper.startPage(pageBo.getPage(), pageBo.getPageSize(), MySqlUtil.orderBy(pageBo.getSort(), pageBo.isAsc()));
        PageInfo<MobileRechargeConfig> info = new PageInfo(mobileRechargeConfigDao.getList(pageBo.getParam()));
        List<MobileRechargeConfigVo> list = BeanConverUtils.convertBeanList(info.getList(),MobileRechargeConfigVo.class);
        return new PageVo<>(info.getTotal(),pageBo.getPage(),pageBo.getPageSize(),list);
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(MobileRechargeConfigBo bo) {
        MobileRechargeConfig exist=getByItemId(bo.getItemId());
        ParamException.isTrue((null!=exist && !exist.getId().equals(bo.getId())),"该编号商品已存在");
        MobileRechargeConfig entity=BeanConverUtils.convertBean(bo,MobileRechargeConfig.class);
        mobileRechargeConfigDao.updateByPrimaryKeySelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(String id) {
        mobileRechargeConfigDao.deleteById(id);
    }

    public MobileOperatorConfigVo getMobileOperatorConfigByMobile(String mobile){
        MobileNumInfo mobileNumInfo=null;
        if (StrUtil.isNotEmpty(mobile) && mobile.length() > 10) {
            mobileNumInfo = mobileNumInfoService.getByMobile(mobile);
        }
        Byte operator =null;
        if (null != mobileNumInfo) {
            operator = mobileNumInfo.getOperator();
        }
        if (null == operator) {
            operator = getOperator(mobile);
        }
        MobileOperatorConfigVo mobileOperatorConfigVo=new MobileOperatorConfigVo();
        //运营商编号改变，兼容以前版本，下期去除
        if (1 == operator.intValue()) {
            mobileOperatorConfigVo.setOperator((byte) 3);
        } else if (2 == operator.intValue()) {
            mobileOperatorConfigVo.setOperator((byte) 1);
        } else if (3 == operator.intValue()) {
            mobileOperatorConfigVo.setOperator((byte) 2);
        }else{
            mobileOperatorConfigVo.setOperator((byte) 4);
        }

        mobileOperatorConfigVo.setMobileRechargeConfigVos(listByMobile(operator));
        return mobileOperatorConfigVo;
    }

    public List<MobileRechargeConfigVo> listByMobile(Byte operator) {
        return mobileRechargeConfigDao.queryByOperator(operator);
    }

    private Byte getOperator(String mobile) {
        return operatorConfigService.getOperatorByMobile(mobile);
    }

    @Transactional(rollbackFor = Exception.class)
    public void enableOrDisable(MobileRechargeConfigBo bo) {
        MobileRechargeConfig entity = new MobileRechargeConfig();
        entity.setId(bo.getId());
        entity.setEnable(bo.getEnable());
        mobileRechargeConfigDao.updateByPrimaryKeySelective(entity);
    }

    public List<MobileRechargeConfig> getByFacePriceExcludeItemId(Integer facePrice, String itemId) {
        MobileRechargeConfig entity = new MobileRechargeConfig();
        entity.setFacePrice(facePrice);
        entity.setItemId(itemId);
        return mobileRechargeConfigDao.getByFacePriceExcludeItemId(entity);
    }

    public MobileRechargeConfig getByFacePriceAndOperator(Integer facePrice, Byte operator) {
        MobileRechargeConfig entity = new MobileRechargeConfig();
        entity.setFacePrice(facePrice);
        entity.setOperator(operator);
        return mobileRechargeConfigDao.selectOne(entity);
    }
}
